package app.auto.runner.base.widget.filter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.aliang.auto.R;

import java.util.ArrayList;
import java.util.List;

public class FilterAdapter extends BaseAdapter implements Filterable {
        DataFilter filter;
        ArrayList<String> data = new ArrayList();

    ArrayList<String> all;

    public FilterAdapter setAll(List all) {
        this.all = (ArrayList<String>) all;
        data.addAll(all);
        return this;
    }

    @Override
        public int getCount() {
            if (data != null && data.size() > 0) {
                return data.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            if (convertView == null) {
                convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.auto_string_item, null);
            }
            ((TextView)convertView.findViewById(R.id.name)).setText((CharSequence) getItem(position));
            return convertView;
        }

        @Override
        public Filter getFilter() {
            if (filter == null) {
                filter = new DataFilter();
                filter.setAll(all);
            }
            return filter;
        }

        private class DataFilter extends Filter {

            private List<String> allData;

            public void setAll(List<String> allData) {
                this.allData = allData;
            }

            @Override
            protected FilterResults performFiltering(CharSequence prefix) {
                //查询结果保存到FilterResults对象里
                FilterResults results = new FilterResults();
                List<String> queryData = new ArrayList<>();
                if(prefix==null){
                    results.values = queryData;
                    results.count = queryData.size();
                    return results;
                }
                for (int i = 0; i < allData.size(); i++) {
                    if (allData.get(i).contains(prefix.toString())) {
                        queryData.add(allData.get(i));
                    }
                }
                results.values = queryData;
                results.count = queryData.size();
                return results;
            }

            /**
             * 更新UI
             */
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                List<String> queryData = (List<String>) results.values;
                if (data == null) {
                    data = new ArrayList<>();
                }
                if (data.size() > 0) {
                    data.clear();
                }

                if (queryData != null && queryData.size() > 0)
                    for (String string : queryData) {
                        data.add(string);
                        notifyDataSetChanged();
                    }
            }

        }
    }